package com.easy.work.function.me;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.base.syslib.pulltorefresh.PullToRefreshBase;
import com.base.syslib.pulltorefresh.PullToRefreshListView;
import com.base.syslib.util.DensityUtil;
import com.base.syslib.util.MessageTools;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.bean.WorkListBean;
import com.easy.work.function.me.adapter.MyWorkListAdapter;
import com.easy.work.function.me.adapter.WorkCheckConditionAdapter;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.WorkFilterRequest;
import com.easy.work.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhangfeng
 * @date 2017/12/26
 * @desc 我的工作
 */

public class MyWorkListActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ListView> {

    private static final String TAG = MyWorkListActivity.class.getSimpleName();

    @BindView(R.id.gv)
    NoScrollGridView gridView;
    @BindView(R.id.listView)
    PullToRefreshListView refreshListView;

    private int status = 1;
//    private int page = 0;

    private WorkCheckConditionAdapter mConditionAdapter;
    private MyWorkListAdapter mWorkAdapter;

    private String[] conditionArr = {"已投递", "待面试", "待入职", "已结束"};

    public static void start(Context ctx) {
        Intent intent = new Intent(ctx, MyWorkListActivity.class);
        ctx.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_work_list);
        ButterKnife.bind(this);
        initTitle("我的工作");

        initGridView();
        initListView();

        AppSession.setPullFormartRefreshTime(refreshListView);
        refreshListView.doPullRefreshing(true, 0);
    }

    private void initGridView() {
        List<String> list = new ArrayList<>();
        list.addAll(Arrays.asList(conditionArr));
        mConditionAdapter = new WorkCheckConditionAdapter(this, 1, list);
        gridView.setAdapter(mConditionAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position != mConditionAdapter.getSelect()) {
                    mConditionAdapter.updateSelect(position);
                    status = position + 1;
                    refreshListView.doPullRefreshing(true, 0);
                }
            }
        });
    }

    /**
     * ListView初始化
     */
    private void initListView() {
        refreshListView.setPullLoadEnabled(true);
        refreshListView.setPullRefreshEnabled(true);
        refreshListView.setOnRefreshListener(this);

        ListView listView = refreshListView.getRefreshableView();
        listView.setDivider(new ColorDrawable(0xffF5F5F5));
        listView.setDividerHeight(DensityUtil.dip2px(this, 10));

        List<WorkListBean.ListEntity> list = new ArrayList<>();
        mWorkAdapter = new MyWorkListAdapter(this, 1, list);
        listView.setAdapter(mWorkAdapter);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mWorkAdapter.clearData();
        getWorkList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        getWorkList();
    }

    private void getWorkList() {
        final WorkFilterRequest request = new WorkFilterRequest();
        request.userId = AppSession.getInstance().getUserId();
        request.status = status;
        request.start = mWorkAdapter.getCount();
        request.limit = 10;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();
                handleData(response);
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();
                MessageTools.showToast(getApplicationContext(), msg);
            }
        });
    }

    /**
     * 数据处理
     */
    private void handleData(BaseResponse response) {
        boolean refreshNumber = true;
        WorkListBean bean = (WorkListBean) response;
        if (bean != null && bean.list != null && !bean.list.isEmpty()) {
            mWorkAdapter.addMoreData(bean.list);
        } else {
            if (mWorkAdapter.getCount() == 0) {
                mWorkAdapter.clearData();
                MessageTools.showToast(getApplicationContext(), "暂无数据");
            } else {
                refreshNumber = false;
                MessageTools.showToast(getApplicationContext(), "没有更多数据了");
            }
        }

        if (status > 0 && refreshNumber) {
            List<String> tempList = new ArrayList<>();
            for (int i = 0; i < conditionArr.length; i++) {
                if (i == status - 1 && mWorkAdapter.getCount() > 0) {
                    tempList.add(conditionArr[i] + "(" + mWorkAdapter.getCount() + ")");
                } else {
                    tempList.add(conditionArr[i]);
                }
            }
            mConditionAdapter.refresh(tempList);
        }
    }
}
